package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCoorCommSeacherData;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CoorSeacherCommActivity extends BaseActivity {
    private TimePickerView mpv1;
    private TimePickerView mpv2;
    String stime;
    String etime;
    TextView stimetv,etimetv;
    private ImageView img_commit;
    Date date1,date2;
    int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coor_seacher_comm);
        stimetv = (TextView) findViewById(R.id.start_time);
        etimetv = (TextView) findViewById(R.id.end_time);
        img_commit= (ImageView) findViewById(R.id.health_education_img_commit);
        id = getIntent().getIntExtra("id",0);
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 =date;
                stime=getTime(date);
                stimetv.setText(stime);
            }
        });
        mpv2 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv2.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv2.setTime(new Date());
        //选择监听
        mpv2.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                etime=getTime(date);
                etimetv.setText(etime);
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (date1!=null&&date2!=null&&!date1.after(date2)){
                    Toast.makeText(CoorSeacherCommActivity.this, "开始时间不能在结束时间之后", Toast.LENGTH_SHORT).show();

                }else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            JSONObject object = new JSONObject();
                            dockDao dockDao=new dockDaoImpl();
                            try {
                                object.put("KXA01",id);
                                Log.d("aa",id+"");
                                object.put("KGA04_B",stime);
                                object.put("KGA04_E",etime);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String json = object.toString();
                            GetCoorCommSeacherData.creat();
                            GetCoorCommSeacherData.list= dockDao.queryCoordingCommunicateMessage(baseClass.find_coording_communication+1,json);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (GetCoorCommSeacherData.list!=null){
                                        finish();
                                    }else {
                                        Toast.makeText(CoorSeacherCommActivity.this, "此条件下查询不到信息", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.health_education_img_back:
                finish();
                break;
            case R.id.start_show:
                mpv1.show();
                break;
            case R.id.end_show:
                mpv2.show();
                break;
        }
    }
}
